import pygame, sys, math, random

class Enemy():
    def __init__(self, images, speed = [5,5], size = [100,100], pos = (0,0)):
        self.images = []
        for image in images:
            newimage = pygame.image.load("rsc/enemie/enemy1.png")
            newimage = pygame.transform.scale(newimage, size)
            self.images += [newimage]
        self.frame = 0
        Enemy.living = True
        self.maxFrame = len(self.images)-1
        self.waitCount = 0
        self.waitMax = 10
        self.image = self.images[self.frame]
        self.rect = self.image.get_rect()
        self.maxSpeedx = speed[0]
        self.maxSpeedy = speed[1]
        self.speedx = 0
        self.speedy = 0
        self.speed = [self.speedx, self.speedy]
        self.radius = self.rect.width/2
        self.place(pos)
        self.wallright = False
        self.wallleft = False
        self.walldown = False
        self.wallup= False
        
    def animate(self):
        if self.waitCount < self.waitMax:
            self.waitCount += 1
        else:
            self.waitCount = 0
            if self.frame < self.maxFrame:
                self.frame += 1
            else:
                self.frame = 0
            self.image = self.images[self.frame]
   
    def collideWall(self, width, height):
        #print "wall collide", width, height
        if self.rect.left < 0 or self.rect.right > width:
           self.speedx = 0
           #print"hit x", self.rect.left, self.rect.right
            
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speedy = 0
            #print"hit y", self.rect.top, self.rect.bottom   
    
    # def colidewallsegment(self,wall):
        # print self.rect.top, wall.rect.bottom
        # print self.rect.bottom, wall.rect.top
        # print self.rect.right, wall.rect.left
        # print self.rect.left, wall.rect.right
        # if self.rect.top < wall.rect.bottom:
            # self.wallup = True
            # print "up"
        # if self.rect.bottom > wall.rect.top:
            # self.walldown = True
            # print "d"
        # if self.rect.right > wall.rect.left:
            # self.wallright = True
            # print "r"
        # if self.rect.left < wall.rect.right:
            # self.wallleft = True
            # print "l"
            
    def colidewallsegment(self,wall):
        if self.rect.right > wall.rect.left and self.rect.left < wall.rect.right:
            if self.rect.bottom > wall.rect.top and self.rect.top < wall.rect.bottom:
                if self.rect.center[0] > wall.rect.center[0]:
                    self.wallright = True
                if self.rect.center[0] < wall.rect.center[0]:
                    self.wallleft = True
                if self.rect.center[1] > wall.rect.center[1]:
                    self.wallup = True
                if self.rect.center[1] < wall.rect.center[1]:
                    self.walldown = True
            
    def place(self, pos):
        self.rect.center = pos
   
    def collideKnife(self, knife):
        if self.rect.right > knife.rect.left and self.rect.left < knife.rect.right:
            if self.rect.bottom > knife.rect.top and self.rect.top < knife.rect.bottom:
                print ">ERTRTG$%^Y%&^H^$H"
                self.living = False
                    
                        
        
    
    def moveEnemy(self, playerPos):
        print self.wallup
        print self.walldown
        print self.wallright
        print self.wallleft
        if self.rect.y > playerPos[1]:
            if not self.wallup:
                self.speedy = -self.maxSpeedy
                print "moving up"
            else:
                self.speedy = 0
        elif self.rect.y < playerPos[1]:
            if not self.walldown:
                self.speedy = self.maxSpeedy
                print "moving down"
            else:
                self.speedy = 0
        else:
            self.speedy = 0
            
        if self.rect.x > playerPos[0]:
            if not self.wallleft:
                self.speedx = -self.maxSpeedx
                print "moving left"
            else:
                self.speedx = 0
        elif self.rect.y < playerPos[0]:
            if not self.wallright:
                self.speedx = self.maxSpeedx
                print "moving right"
            else:
                self.speedx = 0
        else:
            self.speedx = 0
        
        self.rect = self.rect.move([self.speedx, self.speedy])
   
        
    def update(self):
        self.animate()
        self.wallup = False
        self.walldown = False
        self.wallright = False
        self.wallleft = False
        
        
        
   